package com.konsung.ksdc.manager.machine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.MachineNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.ToastUtils;
import com.konsung.ksdc.config.NetConfig;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 一体机详情页面
 * Created by Administrator on 2016/3/15 0015.
 */
public class MyMachineDetailActivity extends BaseActivity {

    @Bind(R.id.tv_machine_name)
    TextView tvName;
    @Bind(R.id.tv_machine_serialnum)
    TextView tvSerialName;
    @Bind(R.id.tv_machine_type)
    TextView tvType;
    @Bind(R.id.tv_product_addr)
    TextView tvAddr;
    @Bind(R.id.tv_product_time)
    TextView tvTime;
    @Bind(R.id.tv_machine_deadline)
    TextView tvDeadLine;
    @Bind(R.id.btn_remove_associate)
    Button btnRemoveAssociate;
    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_machine_detail);
        ButterKnife.bind(this);

        final MachineNetBean bean = getIntent().getParcelableExtra(MyMachineActivity
                .CURRENT_MACHINE_NETBEAN);
        initView(bean);

        btnRemoveAssociate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                relieveMac(NetConfig.currentEmpid, bean.getId());
            }
        });
    }

    @Override
    protected String activityDesc() {
        return "一体机详情页面";
    }

    /**
     * 解除关联
     * @param empId 账户empId
     * @param macId 一体机id
     */
    private void relieveMac(String empId, String macId) {
        if (!RequestUtils.checkNetwork(MyMachineDetailActivity.this)) {
            return;
        }
        RequestUtils.derelateMac(empId, macId, new RequestCallback() {
            @Override
            public void onError(Call call, Exception e) {
                ToastUtils.toastContent(MyMachineDetailActivity.this,
                        getString(R.string.chat_unconnect));
            }

            @Override
            public void onResponse(ResponseBean responseBean) {
                String resultCode = responseBean.getResultCode();
                if (resultCode.equals(Constants.REQUEST_OK)) {
                    // ### 解除关联成功 ###
                    Toast.makeText(MyMachineDetailActivity.this,
                            responseBean.getEntity(String.class),
                            Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(MyMachineDetailActivity.this,
                            responseBean.getEntity(String.class),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 初始化一体机详情页面
     * @param bean 一体机数据Bean
     */
    private void initView(MachineNetBean bean) {
        tvTitle.setText(R.string.title_my_machine_detail); // title栏
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 数据
        tvName.setText(bean.getMacName());
        tvSerialName.setText(bean.getSerialNum());
        tvAddr.setText(bean.getProcAddr());
        tvDeadLine.setText(bean.getWarrantyDate());
        tvTime.setText(bean.getProcDate());
        tvType.setText(bean.getMacStyle());
    }
}
